package org.mwielocha.jxcube.ut;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class CustomTable extends ControlAdapter {
  
  protected Composite parent;
  protected Table table;
  
  protected List<TableColumn> columns;
  protected List<Integer> weights;
  
  public CustomTable(Composite parent, int style) {
    this.parent = parent;
    columns = new ArrayList<TableColumn>();
    weights = new ArrayList<Integer>();
    createControl(style);
  }
  
  protected void createControl(int style) {
    table = new Table(parent, style);
   // parent.addControlListener(this);
  }
  
  public void addColumn(int style, String label, int weight) {
    TableColumn column = new TableColumn(table, style);
    column.setText(label);
    columns.add(column);
    weights.add(weight);
  }
  
  public void addSelectionListener(SelectionListener l) {
    table.addSelectionListener(l);
  }
  
  public void setHeaderVisible(boolean headerVisible) {
    table.setHeaderVisible(headerVisible);
  }
  
  public void setLinesVisible(boolean linesVisible) {
    table.setLinesVisible(linesVisible);
  }
  
  public int getSelectionIndex() {
    return table.getSelectionIndex();
  }
  
  public TableItem getItem(int idx) {
    return table.getItem(idx);
  }
  
  public Table getTable() {
    return table;
  }
  
  public void controlResized(ControlEvent e) {
    Rectangle area = parent.getClientArea();
    Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    ScrollBar vBar = table.getVerticalBar();
    int width = area.width - table.computeTrim(0,0,0,0).width - vBar.getSize().x;
    if (size.y > area.height + table.getHeaderHeight()) {
      // Subtract the scrollbar width from the total column width
      // if a vertical scrollbar will be required
      Point vBarSize = vBar.getSize();
      width -= vBarSize.x;
    }
    Point oldSize = table.getSize();
    if (oldSize.x > area.width) {
      // table is getting smaller so make the columns 
      // smaller first and then resize the table to
      // match the client area width
      int sum = 0;
      for(int i = 0; i < columns.size() - 1; i++) {
        columns.get(i).setWidth(width / weights.get(i));
        sum = sum + columns.get(i).getWidth();
      }
      
      columns.get(columns.size()).setWidth(width - sum);
      
      table.setSize(area.width, area.height);
    } else {
      // table is getting bigger so make the table 
      // bigger first and then make the columns wider
      // to match the client area width
      table.setSize(area.width, area.height);
      int sum = 0;
      for(int i = 0; i < columns.size() - 1; i++) {
        columns.get(i).setWidth(width / weights.get(i));
        sum = sum + columns.get(i).getWidth();
      }
      
      columns.get(columns.size()).setWidth(width - sum);
    }
  }
}
